package cn.pupil.springframework.util;

import java.util.concurrent.ExecutionException;

/**
 * @Description: class 工具类
 * @Author: pupil
 * @Date: 2024/11/4 下午 4:26
 */
public class ClassUtils {

    public static ClassLoader getDefaultClassLoader() {
        ClassLoader classLoader = null;
        try {
            // 获取当前线程的内容加载器
            classLoader = Thread.currentThread().getContextClassLoader();
        } catch (Throwable ex) {
            new ExecutionException("Cannot access thread context ClassLoader - falling back to system class loader...", ex);
        }
        if (classLoader == null) {
            // 获取ClassUtils的加载器
            classLoader = ClassUtils.class.getClassLoader();
        }
        return classLoader;
    }

    /**
     * Check whether the specified class is a CGLIB-generated class.
     * 检查指定的类是否是CGLIB生成的类.
     * @param clazz the class to check
     */
    public static boolean isCglibProxyClass(Class<?> clazz) {
        return (clazz != null && isCglibProxyClassName(clazz.getName()));
    }

    /**
     * Check whether the specified class name is a CGLIB-generated class.
     * 检查指定的类是否是CGLIB生成的类.
     * 代理类的例子：
     * cn.pupil.springframework.bean.UserService$$EnhancerByCGLIB$$51a155cc.<init>(java.lang.String)
     * CglibProxy的代理对象的className存在$$字符
     * @param className
     * @return
     */
    public static boolean isCglibProxyClassName(String className) {
        return (className != null && className.contains("$$"));
    }


}
